﻿using _15Net8AspNetCore_ORM.Context;
using _15Net8AspNetCore_ORM.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace _15Net8AspNetCore_ORM.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        public SqlSugarDBContext _SqlSugarContext { get; set; }
        public OrderController(SqlSugarDBContext sqlSugarContext)
        {
            _SqlSugarContext = sqlSugarContext;
        }

        /// <summary>
        /// 查询单个订单
        /// </summary>
        /// <returns></returns>
        [HttpGet("id")]
        public IActionResult GetOrder(string id)
        {
            Order order = _SqlSugarContext._SqlSugarClient.Queryable<Order>().InSingle(id);
            return Ok(order);
        }

        /// <summary>
        /// 查询所有订单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetOrder()
        {
            List<Order> orders = _SqlSugarContext._SqlSugarClient.Queryable<Order>().ToList();
            return Ok(orders);
        }

        /// <summary>
        /// 新增订单
        /// </summary>
        [HttpPost]
        public IActionResult AddProduct(Order order)
        {
            order.Id = _SqlSugarContext._SqlSugarClient.Insertable(order).ExecuteCommand();
            return Ok("添加成功");
        }

        /// <summary>
        /// 删除订单
        /// </summary>
        [HttpDelete]
        public IActionResult DeleteProduct(int id)
        {
            _SqlSugarContext._SqlSugarClient.Deleteable<Order>(id).ExecuteCommand();
            return Ok("删除成功");
        }

        /// <summary>
        /// 修改订单
        /// </summary>
        [HttpPut]
        public IActionResult UpdateProduct(Order order)
        {
            _SqlSugarContext._SqlSugarClient.Updateable<Order>(order).ExecuteCommand();
            return Ok("修改成功");
        }
    }
}
